package top.jacktgq.closure

/**
 @Author CandyWall
 @Date 2021/4/27--1:26
 @Description 闭包定义
 */
// 定义一个闭包
def closure1 = {
    println "Hello groovy"
}
closure1.call()
closure1()

// 定义带一个参数的闭包
def closure2 = {
    String name -> println "Hello ${name}"
}
closure2.call("Candywall")
closure2("Candywall")

// 定义带多个参数的闭包
def closure3 = {
    String name, int age -> println "Hello ${name}, My age is ${age}"
}
closure3.call("zhangsan", 16)
closure3("lisi", 20)

// 闭包支持一个默认参数，it
def closure4 = {
    println "Hello ${it}"
}
closure4.call("zhangsan")
closure4("lisi")

// 闭包返回值
def closure5 = {
    String name ->
    return "hello ${name}"
}
def sayHello1 = closure5("wangwu")
println sayHello1

// 如果不加返回语句，默认返回null
def closure6 = {
    String name ->
    println "hello ${name}"
}
def sayHello2 = closure6("zhaoliu")
println sayHello2

